var tid = '';
var wname = '';
$(function() {

	$("#workflowName").selectable({
		stop : function(event, ui) {

			// var workflow = this.get(0);
			// alert(ui);
			// result.append(workflow.val());
		},
		selected : function(event, ui) {
			var sel = ui['selected'];
			var result = $("#workflowresult");
			result.empty();
			wname = sel.attributes['wname']['value'];
			result.append("您选择的工作流是:" + wname);
			$.blockUI({
				message : '正在处理,请稍候...'
			});
			$.ajax({
				type : 'POST',
				url : navObj.form.core+navObj.form.byworkflow,
				data : 'name.workflowName=' + wname,
				success : function(data) {
					$.unblockUI();
					$("#tempmsg").html("当前关联的模板是  : "+((data!=null&&data.title)?data.title:"没有关联模板"));
				},
				error : function(msg) {
					$("#tempmsg").html(msg);
					$.unblockUI();
				}

			});

		}

	});

	$("#formTmpId").selectable({
		stop : function(event, ui) {

		},
		selected : function(event, ui) {
			var sel = ui['selected'];
			var result = $("#templateresult");
			result.empty();
			// debugObject(sel);
			// alert(sel.textContent);

			result.append("您选择的模板是:" + sel.attributes['title']['value']);
			tid = sel.attributes['tid']['value'];

		}
	});
	// $.ajax({});
	// ${base}relateform.action
	$("#rltBtn").bind("click",function(){
		relateform(true);		
	}).button();
	$("#unrltBtn").bind("click",function(){
		relateform(false);		
	}).button();
});
function relateform(flag) {
	if (tid == '') {
		alert('请选择模版');
		return;
	}
	if (wname == '') {
		alert('请选择工作流');
		return;
	}
	$.blockUI({
		message : '正在处理,请稍候...'
	});
	var param = {
			"bean.id" : tid,
			"name.workflowName" : wname
	};
	if(!flag)delete param['bean.id'];
	var url = navObj.form.core+navObj.form.relate;
	// alert($.param(param));
	$.ajax({
		url : url,
		type : 'POST',
		data : $.param(param),
		success : function(msg) {
			// alert(msg);
			$.unblockUI();
			createNotify('关联完成!');
		},
		error : function(msg) {
			$.unblockUI();
			alert('关联错误:' + msg);
		}
	});
}